package data.implementation;

import data.IMedicamentDAO;
import data.MySingleton;
import exception.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import model.Medicament;
import model.SoinJournalier;

/**
 *
 * @author BACQ Justine et SACRE Sauvane
 */
public class MedicamentDBAccess implements IMedicamentDAO {

    private static final String prescritionSoinJ = "SELECT m.* "
            + "FROM Prescription p, Medicament m "
            + "WHERE p.idSoinPrescription = ? "
            + "AND p.dateDebutSoinPrescription = ? "
            + "AND p.heureDebutSoinPrescription = ? "
            + "AND p.soigneurPrescription = ? "
            + "AND p.dateDebutSessionPrescription = ? "
            + "AND p.heureDebutSessionPrescription = ? "
            + "AND p.idMedicamentPrescription = m.idMedicament";

    @Override
    public List<Medicament> getMedicamentPrescritSession(SoinJournalier soinJSession) throws ExceptionConnection, ExceptionPrescription, MedException {
        List<Medicament> allMedic = new ArrayList<Medicament>();
        Connection con = MySingleton.getInstance();
        try {
            PreparedStatement prepStat = con.prepareStatement(prescritionSoinJ);
            prepStat.setString(1, soinJSession.getSoinFait().getIdSoin());
            prepStat.setDate(2, new java.sql.Date(soinJSession.getDateDebutSoin().getTime()));
            prepStat.setTime(3, new java.sql.Time(soinJSession.getHeureDebutSoin().getTime()));
            prepStat.setString(4, soinJSession.getPeriode().getPersonne().getIdSoigneur());
            prepStat.setDate(5, new java.sql.Date(soinJSession.getPeriode().getDateDebut().getTime()));
            prepStat.setTime(6, new java.sql.Time(soinJSession.getPeriode().getHeureDebut().getTime()));

            ResultSet res = prepStat.executeQuery();

            while (res.next()) {
                Integer idMedic = res.getInt("idMedicament");
                String nomMedic = res.getString("nomMedicament");
                Medicament medic = new Medicament(idMedic, nomMedic);
                allMedic.add(medic);
            }
            
        } catch (SQLException ex) {
            throw new MedException();
        }
        return allMedic;
    }
}
